﻿<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Buffalo.Lib.Common" %>
<%@ Import Namespace="Buffalo.Tourism.DAL.Models" %>
<%@ Import Namespace="Buffalo.Tourism.DAL" %>
<%@ Import Namespace="Buffalo.Web.Areas.Tourism" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
    </script>
    <script runat="server">
        protected void ImportClick(object sender, DirectEventArgs e)
        {
            if (this.filImportExcel.HasFile)
            {
                string excelFile = this.filImportExcel.PostedFile.FileName;
                if (!excelFile.EndsWith(".xls"))
                {
                    ShowMessage("Error", "File type is not valid!", MessageBox.Icon.ERROR);
                    return;
                }
                string connectionString = ConfigurationManager.ConnectionStrings[GeneralConstant.CONNECTION_STRING].ConnectionString;
                X.Msg.Show(new MessageBoxConfig
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "Success",
                    Message = string.Format("Imported file: {0}", excelFile)
                });
                string excelFullFile = string.Format(@"{0}\Temp\{1}", CommonManager.GetModuleFolder(), excelFile);
                filImportExcel.PostedFile.SaveAs(excelFullFile);
                TourProgramDAL tourProgramDAL = new TourProgramDAL();
                tourProgramDAL.ImportTourFromExcel(excelFullFile, connectionString, GeneralVar.gLoggedRoleName);

            }
            else
            {
                ShowMessage("Error", "Import fail", MessageBox.Icon.ERROR);
            }
        }
        private void ShowMessage(string title, string message, MessageBox.Icon icon)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Buttons = MessageBox.Button.OK,
                Icon = icon,
                Title = title,
                Message = message
            });
        }
    </script>
</head>
<body>
    <ext:ResourceManager ID="ResourceManager1" runat="server">
    </ext:ResourceManager>
    <form id="Form1" runat="server">
    <ext:Viewport ID="mainViewPort" runat="server" Layout="fit">
        <Items>
            <ext:Panel ID="Panel1" runat="server" Border="false" Layout="fit">
                <TopBar>
                    <ext:Toolbar ID="Toolbar1" runat="server">
                        <Items>
                            <ext:Button ID="btnImport" runat="server" Text="Import" Icon="PageExcel">
                                <DirectEvents>
                                    <Click OnEvent="ImportClick">
                                    </Click>
                                </DirectEvents>
                            </ext:Button>
                            <ext:ToolbarFill ID="ToolbarFill1" runat="server" />
                        </Items>
                    </ext:Toolbar>
                </TopBar>
                <Items>
                    <ext:Panel ID="panTourInfo" runat="server" Padding="6" Layout="fit">
                        <Items>
                            <ext:FormPanel ID="ImportExcelForm" runat="server" Border="false">
                                <Items>
                                    <ext:Container ID="Container1" runat="server" Flex="1">
                                        <Items>
                                            <ext:FileUploadField ID="filImportExcel" runat="server" Width="300" Icon="Attach"
                                                EmptyText="Select an Excel file" />
                                        </Items>
                                    </ext:Container>
                                </Items>
                            </ext:FormPanel>
                        </Items>
                    </ext:Panel>
                </Items>
            </ext:Panel>
        </Items>
    </ext:Viewport>
    </form>
</body>
</html>
